import KSSDK from "./KSSDK";
import PlatfromEnum from "./PlatfromEnum";
import TTSDK from "./TTSDK";
import WXSDK from "./WXSDK";

export default class SDKHelper {
    private static _instance: SDKHelper = null;
    public static get Instance(): SDKHelper {
        this._instance = this._instance || new SDKHelper();
        return this._instance;
    }

    private sdk_platform: number;
    private _isMiniGame: boolean;
    public get isMiniGame(): boolean {
        return this._isMiniGame;
    }
    public set isMiniGame(bool: boolean) {
        this._isMiniGame = bool
    }

    public initSdk() {
        this.sdk_platform = this.getPlatfromEnum();
        console.log("platform:" + this.sdk_platform);

        TTSDK.Instance.init();
        WXSDK.Instance.init();
    }

    private getPlatfromEnum(): number {
        let result: number;
        this._isMiniGame = true;
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            result = PlatfromEnum.WX;
        } else if (cc.sys.platform === cc.sys.BYTEDANCE_GAME) {
            result = PlatfromEnum.TT;
        } else if (typeof globalThis.KSGameGlobal != 'undefined') {
            result = PlatfromEnum.KS;
        } else {
            result = PlatfromEnum.WEB;
            this._isMiniGame = false;
        }
        return result;
    }

    public share(options: { success?: Function, fail?: Function }) {
        switch (this.sdk_platform) {
            case PlatfromEnum.TT:
                TTSDK.Instance.share(options);
                break;
            case PlatfromEnum.WX:
                WXSDK.Instance.share(options);
                break;
            case PlatfromEnum.KS:
                KSSDK.Instance.share(options);
                break;
            default:
                console.log("<分享> 不处于可分享平台");
                break;
        }
    }

    public showBanner() {
        switch (this.sdk_platform) {
            case PlatfromEnum.TT:
                TTSDK.Instance.showBanner();
                break;
            case PlatfromEnum.WX:
                WXSDK.Instance.showBanner();
                break;
            case PlatfromEnum.KS:
                console.log("<banner广告> 快手平台无banner");
                break;
            default:
                console.log("<banner广告> 所在平台无法显示");
                break;
        }
    }

    public hideBanner(){
        switch (this.sdk_platform) {
            case PlatfromEnum.TT:
                TTSDK.Instance.hideBanner();
                break;
            case PlatfromEnum.WX:
                WXSDK.Instance.hideBanner();
                break;
            case PlatfromEnum.KS:
                console.log("<banner广告> 快手平台无banner");
                break;
            default:
                console.log("<banner广告> 所在平台无法显示");
                break;
        }
    }

    public showRewardVideo(options: { success: Function, fail: Function }) {
        switch (this.sdk_platform) {
            case PlatfromEnum.TT:
                TTSDK.Instance.showRewardVideo(options);
                break;
            case PlatfromEnum.WX:
                WXSDK.Instance.showRewardVideo(options);
                break;
            case PlatfromEnum.KS:
                KSSDK.Instance.showRewardVideo(options);
                break;
            default:
                console.log("<激励视频广告> 不处于小游戏平台，直接成功");
                options.success && options.success();
                break;
        }
    }

    public showInterstitial() {
        switch (this.sdk_platform) {
            case PlatfromEnum.TT:
                TTSDK.Instance.playScreen();
                break;
            case PlatfromEnum.WX:
                WXSDK.Instance.playScreen();
                break;
            case PlatfromEnum.KS:
                KSSDK.Instance.playScreen();
                break;
            default:
                console.log("<插屏广告> 不处于小游戏平台");
                break;
        }
    }

    public vibrateShort() {
        switch (this.sdk_platform) {
            case PlatfromEnum.TT:
                TTSDK.Instance.vibrateShort();
                break;
            case PlatfromEnum.WX:
                WXSDK.Instance.vibrateShort();
                break;
            case PlatfromEnum.KS:
                KSSDK.Instance.vibrateShort();
                break;
            default:
                // console.log("<短震动> 不处于小游戏平台");
                break;
        }
    }

    public vibrateLong() {
        switch (this.sdk_platform) {
            case PlatfromEnum.TT:
                TTSDK.Instance.vibrateLong();
                break;
            case PlatfromEnum.WX:
                WXSDK.Instance.vibrateLong();
                break;
            case PlatfromEnum.KS:
                KSSDK.Instance.vibrateLong();
                break;
            default:
                console.log("<长震动> 不处于小游戏平台");
                break;
        }
    }

    public getShakeState(): boolean {
        let state: boolean;
        switch (this.sdk_platform) {
            case PlatfromEnum.TT:
                state = TTSDK.Instance.getShakeState();
                break;
            case PlatfromEnum.WX:
                state = WXSDK.Instance.getShakeState();
                break;
            case PlatfromEnum.KS:
                state = KSSDK.Instance.getShakeState();
                break;
            default:
                state = false;
                break;
        }
        return state;
    }

    public setShakeState(bool: boolean) {
        switch (this.sdk_platform) {
            case PlatfromEnum.TT:
                TTSDK.Instance.setShakeState(bool);
                break;
            case PlatfromEnum.WX:
                WXSDK.Instance.setShakeState(bool);
                break;
            case PlatfromEnum.KS:
                KSSDK.Instance.setShakeState(bool);
                break;
            default:

                break;
        }
    }

    public subpackage(name: string, callback?: Function) {
        switch (this.sdk_platform) {
            case PlatfromEnum.TT:
                TTSDK.Instance.ttSubpackage(name, callback);
                break;
            case PlatfromEnum.WX:
                WXSDK.Instance.wxSubpackage(name, callback);
                break;
            case PlatfromEnum.KS:
                KSSDK.Instance.ksSubpackage(name, callback);
                break;
            default:
                callback();
                console.log("<小游戏分包>  不处于小游戏平台");
                break;
        }
    }

    public ttCheckSidebar(callback?: Function) {
        if (this.sdk_platform !== cc.sys.BYTEDANCE_GAME) {
            callback && callback(false);
        } else {
            TTSDK.Instance.checkSidebar(callback);
        }
    }

    public ttShowSidebar(options: { success?: Function, fail?: Function }) {
        if (this.sdk_platform !== cc.sys.BYTEDANCE_GAME) {
            options.fail && options.fail();
        } else {
            TTSDK.Instance.showSidebar(options);
        }
    }
}
